<template>
  <div class="divBox">
    <el-card class="box-card">
      <el-form inline size="small" @submit.native.prevent>
        <el-form-item label="关键词：">
          <el-input v-model="listPram.keyword" placeholder="姓名|手机号|流水号" clearable class="asWidth"/>
        </el-form-item>
        <el-form-item label="查看类型：">
          <el-select v-model="listPram.ifVI" placeholder="请选择">
            <el-option
                v-for="item in vList"
                :key="item.id"
                :label="item.name"
                :value="item.id" class="selWidth">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型：">
          <el-select v-model="listPram.typeName" placeholder="请选择">
            <el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item.name"
                :value="item.name" class="selWidth">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择时间：">
          <el-date-picker
              v-model="value1"
              @change="handleChange"
              value-format="yyyy-MM-dd" format="yyyy-MM-dd"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width: 300px"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="地区：" prop="areaId">
          <el-cascader class="col"
                       placeholder="可输入地区名称进行搜索"
                       v-model="value"
                       :options="options"
                       clearable
                       :props="{ expandTrigger: 'hover' }"
                       filterable
                       @change="handleChange1"></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click.native="getList">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
        <el-form-item style="margin-left: 30px">
          <span>券流转总量 {{ dataInfo.totalCirculationValue }} 券</span>
          <span style="margin-left: 20px">积分流转总量 {{ dataInfo.totalPointsAndRecords }} 积分</span>
        </el-form-item>
      </el-form>
      <el-table :data="listData.list" size="mini"
                :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}" border>
        <el-table-column label="名称" prop="actName" width="130"/>
        <el-table-column label="手机号" prop="phone" width="130"/>
        <el-table-column label="流水号" prop="orderId" min-width="130"/>
        <el-table-column label="记录类型" prop="typeName" min-width="130"/>
        <el-table-column label="值" prop="integral" min-width="130"/>
        <el-table-column label="时间" prop="time" min-width="150"/>
        <el-table-column label="操作" min-width="180" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button icon="el-icon-edit" size="mini" type="primary" @click="openEdit(scope.row.id,scope.row.ifVI)"
                       v-has-permi="['admin:circulation:detail']">详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          :current-page="listPram.page"
          :page-sizes="[20,40,60,80,100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listData.count"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </el-card>
    <el-dialog
        :visible.sync="editDialog.show"
        destroy-on-close
        :close-on-click-modal="false"
        width="1200px">
      <edit
          v-if="editDialog.show"
          :id="editDialog.id"
          :ifVI="editDialog.ifVI"
          @hideEditDialog="hideEditDialog"
          ref="editForm"
      />
    </el-dialog>
  </div>
</template>

<script>
import edit from './edit.vue'
import {deleteRow, list, updateStatus, voucherRecordStatistics} from "@/api/vip";
import {checkPermi} from "@/utils/permission";
import {optionsList} from "@/api/business";

export default {
  components: {edit},
  data() {
    return {
      listData: {list: [], count: 0},
      listPram: {
        keyword: '',
        beginTime: null,
        endTime: null,
        areaId: '',
        typeName: '',
        page: 1,
        limit: 20,
      },
      value1: '',
      editDialog: {
        show: false,
        id: null,
        ifVI: null
      },
      tableLoading: false,
      vList: [{id: 1, name: "益联券流转记录"}, {id: 2, name: "积分流转记录"}, {id: 3, name: "全部"}],
      typeList: [
        {name: "邀请好友"},
        {name: "抽奖"},
        {name: "益联券发放"},
        {name: "会员开通"},
        {name: "好友转账"},
        {name: "活动抢购"},
        {name: "核销"},
        {name: "取消订单"},
        {name: "扣减"},
        {name: "发放"},
        {name: "返还积分"},
        {name: "签到"},
        {name: "贡献积分(活动抢购)"},
        {name: "兑换"}
      ],
      value: [],
      dataInfo: {},
      options: [{
        value: '',
        label: '',
        children: [{
          value: '',
          label: '',
          children: [{
            value: '',
            label: ''
          }]
        }]
      }],
    }
  },
  mounted() {
    this.getList()
    this.getOptions()
  },
  created() {
    if (this.$route.query.keyword) {
      this.listPram.keyword = this.$route.query.keyword
    }
    if (this.$route.query.typeName) {
      this.listPram.typeName = this.$route.query.typeName
    }
    if (this.$route.query.ifVI) {
      this.listPram.ifVI = this.$route.query.ifVI
      this.editDialog.ifVI = this.$route.query.ifVI
    }

  },
  methods: {
    handleChange1(value) {
      this.listPram.areaId = value[2]
    },
    getOptions() {
      optionsList().then(res => {
        this.options = res
      })
    },
    handleChange(value) {
      if (Array.isArray(value) && value.length === 2) {
        this.listPram.beginTime = value[0];
        this.listPram.endTime = value[1];
      }
    },
    checkPermi,
    getList() {
      this.tableLoading = true
      list(this.listPram).then(res => {
        this.listData = res;
        this.voucherRecordStatistics()
      }).finally(() => {
      })
      this.tableLoading = false;
    },

    voucherRecordStatistics() {
      voucherRecordStatistics(this.listPram).then(res => {

        this.dataInfo = res;
      }).finally(() => {
      })
      this.tableLoading = false;
    },
    openEdit(id = null, ifVI = null) {
      this.editDialog.id = id;
      this.editDialog.ifVI = ifVI;
      this.editDialog.show = true
    },
    handlerDelete(id) {
      this.$confirm('确认删除该数据吗？').then(() => {
        const loading = this.$loading({text: '删除中', background: 'rgba(255,255,255,.5)'})
        deleteRow(id).then(() => {
          this.$message.success('删除成功')
          this.getList()
        }).finally(() => {
          loading.close()
        })
      })
    },
    hideEditDialog() {
      this.editDialog.show = false
      this.getList()
    },
    handleSizeChange(val) {
      this.listPram.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listPram.page = val
      this.getList()
    },
    handleStatusChange(status, id) {
      const loading = this.$loading({text: '更新中', background: 'rgba(255,255,255,.5)'})
      updateStatus(id, status).then(() => {
        this.$message.success('更新状态成功')
        this.getList()
      }).finally(() => {
        loading.close()
      })
    },
    resetQuery() {
      this.listPram.keyword = '';
      this.value1 = '';
      this.listPram.beginTime = null;
      this.listPram.endTime = null;
      this.listPram.ifVI = 3;
      this.listPram.areaId = null;
      this.listPram.typeName = ''
      this.value = ''
      this.getList()
    }
  }
}
</script>

<style scoped lang="scss">
.asWidth {
  width: 200px;
}
</style>
